<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'nls/date.inc.php');


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/te_tcms_exception.class.php');


class TCMS_Category extends Tangra_Class {
	private $id;
	private $hid;
	private $title;
	private $parent;
	private $ordering = 10;
	private $subcategories = array();
	private $last_modified;


	public function set_id($id) {
		tangra_if_not_int_throw_e($id);

		$this->id = $id;
	}


	public function get_id() {
		return $this->id;
	}


	public function set_hid($hid) {
		if (ereg("[a-z0-9_]{1,100}", $hid)) {
			$this->hid = $hid;
		} else {
			throw TE_TCMS_Exception('Invalid hid: '.$hid.'. Must conform ereg("[a-z0-9_]{1,100}").');
		}
	}


	public function get_hid() {
		return $this->hid;
	}


	public function set_title($title) {
		$this->title = $title;
	}


	public function get_title() {
		return $this->title;
	}


	public function set_parent($parent) {
		tangra_if_not_int_throw_e($parent);

		$this->parent = $parent;
	}


	public function get_parent() {
		return $this->parent;
	}


	public function set_ordering($ordering) {
		tangra_if_not_int_throw_e($ordering);

		$this->ordering = $ordering;
	}


	public function get_ordering() {
		return $this->ordering;
	}


	public function subcatregory_exists(TCMS_Category $category) {
		return array_key_exists($category->get_hid(), $this->subcategories);
	}


	public function set_subcategories($subcategories) {
		if (is_array($subcategories)) {
			foreach($subcategories as $item) {
				$this->add_subcategory($item);
			}
		} else {
			throw new TE_TCMS_Exception('$subcategories is not an array.');
		}
	}


	public function add_subcategory(TCMS_Category $category) {
		if (!$this->subcatregory_exists($category)) {
			$this->subcategories[$category->get_hid()] = $category;
		} else {
			throw new TE_TCMS_Exception($category->get_hid());
		}
	}


	public function get_subcategories() {
		return $this->subcategories;
	}


	protected function set_last_modified($last_modified) {
		if (check_timestamp($last_modified)) {
			$this->last_modified = $last_modified;
		} else {
			throw new Tangra_Module_Exception('Invalid datetime in $last_modified: '.$last_modified);
		}
	}


	public function get_last_modified() {
		return $this->last_modified;
	}

}